<template>
    <div class="shopper_container">
        <!-- 购物车列表 -->
        <div class="mui-card carList" v-for="(item, i) in shopCarList" :key="item.id">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <mt-switch v-model="$store.getters.getItemSelected[item.id]"
                    @change="selectedChange(item.id, $store.getters.getItemSelected[item.id])"></mt-switch>
                    <img :src="item.thumb_path">
                    <div>
                        <h1>{{ item.title }}</h1>
                        <p>
                            <span class="price">￥{{ item.sell_price }}</span>
                            <numbox :initCount="$store.getters.getItemCount[item.id]"
                                :id="item.id"></numbox>
                            <a href="#" @click.prevent="remove(item.id, i)">删除</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <!-- 购物车总价 -->
        <div class="mui-card totalPrice">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="left">
                        <p>总价（不包含运费）</p>
                        <p>已勾选商品 <span class="red">{{$store.getters.getAllPrice.count}}</span> 件,  总价<span class="red">￥{{$store.getters.getAllPrice.amount}}</span></p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>   
                </div>
            </div>
        </div>
        <!-- 清空购物车 -->
        <mt-button type="primary" @click="clearAll" plain size="large">清空购物车</mt-button>
    </div>
</template>

<script>
import numbox from '../subcomponents/ShopNumBox.vue'
export default {
    data() {
        return {
            shopCarList: this.$store.state.car //购物车页面数据
        }
    },
    created() {
        this.getShopCarList()
    },
    methods: {
        getShopCarList() {//获取购物车页面数据
            var ids = []
            this.$store.state.car.forEach(element => {
                ids.push(element.id)
            })
            this.$http.get('api/goods/getshopcarlist/' + ids.join(',')).then(res => {
                if(res.body.status === 0) {
                    this.shopCarList = res.body.message
                }
            })
        },
        clearAll() { //清空购物车
            this.shopCarList = []
            this.$store.commit('clearAllCar')
        },
        remove(id, i) { //删除购物车中的商品
            this.shopCarList.splice(i, 1)
            this.$store.commit('removeFromCar', id)
        },
        selectedChange(id, val) {//选中项改变
            this.$store.commit('updateSelected', {id, val})
        }
    },
    components: {
        numbox
    }
}
</script>

<style lang="scss" scoped>
    .shopper_container{
        background-color: #eee;
        overflow: hidden;
        .carList{
            .mui-card-content-inner{
                display: flex;
                align-items: center;
                justify-content: space-between;
            }
            img{
                width: 60px;
                height: 60px;
            }
            h1{
                font-size: 12px;
                text-align: center;
                padding-bottom: 5px;
            }
            p{
                margin: 0;
                padding: 0;
                font-size: 12px;
            }
            span.price{
                color: red;
            }
            span.del{
                color: #0066ff;
            }
        }
        .totalPrice{
            .mui-card-content-inner{
                display: flex;
                justify-content: space-between;
                align-items: center;
                .red{
                    color: red;
                    font-weight: 700;
                }
            }
        }
    }
</style>
